﻿// VsPkg.cs : Implementation of DiveDeeper_HowToPackage
//

using System;
using System.ComponentModel.Design;
using System.Runtime.InteropServices;
using DiveDeeper.HowToPackage.BlogItemEditor;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;

namespace DiveDeeper.HowToPackage
{
  [PackageRegistration(UseManagedResourcesOnly = true)]
  [DefaultRegistryRoot("Software\\Microsoft\\VisualStudio\\9.0")]
  [InstalledProductRegistration(false, "#110", "#112", "1.0", IconResourceID = 400)]
  [ProvideLoadKey("Standard", "1.0", "HowTo", "DiveDeeper", 1)]
  [ProvideMenuResource(1000, 1)]
  [ProvideToolWindow(typeof(LearnVSXNowToolWindow))]
  [ProvideEditorFactory(typeof(BlogItemEditorFactory), 200, TrustLevel = __VSEDITORTRUSTLEVEL.ETL_AlwaysTrusted)]
  [ProvideEditorExtension(typeof(BlogItemEditorFactory), 
    HowToPackage.BlogFileExtension,
    32,
    ProjectGuid = "{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}",
    TemplateDir = @"..\..\BlogItemEditor\Templates",
    NameResourceID = 200)]
  [ProvideEditorLogicalView(typeof(BlogItemEditorFactory), GuidList.GuidBlogItemEditorLogicalView)]
  [Guid(GuidList.GuidHowToPackagePkgString)]
  public sealed class HowToPackage : Package
  {
    #region Constant values

    public const string BlogFileExtension = ".blit";

    #endregion

    #region Private fields

    #endregion

    #region Lifecycle methods

    protected override void Initialize()
    {
      base.Initialize();

      // --- Register the blog item editor
      RegisterEditorFactory(new BlogItemEditorFactory());

      new LearnVSXNowViewCommandHandler(this).Bind();

      //// --- Add our command handlers for menu (commands must exist in the .vsct file)
      //OleMenuCommandService mcs = GetService(typeof(IMenuCommandService)) as OleMenuCommandService;
      //if (null != mcs)
      //{
      //  // Create the command for the tool window
      //  CommandID toolwndCommandID = new CommandID(GuidList.GuidHowToPackageCmdSet, (int)PkgCmdIDList.cmdShowLearnVSXNowTool);
      //  MenuCommand menuToolWin = new MenuCommand(ShowToolWindow, toolwndCommandID);
      //  mcs.AddCommand(menuToolWin);
      //}
    }

    #endregion

    private void ShowToolWindow(object sender, EventArgs e)
    {
      // Get the instance number 0 of this tool window. This window is single instance so this instance
      // is actually the only one.
      // The last flag is set to true so that if the tool window does not exists it will be created.
      ToolWindowPane window = FindToolWindow(typeof(LearnVSXNowToolWindow), 0, true);
      if ((null == window) || (null == window.Frame))
      {
        throw new NotSupportedException(Resources.CanNotCreateWindow);
      }
      IVsWindowFrame windowFrame = (IVsWindowFrame)window.Frame;
      Microsoft.VisualStudio.ErrorHandler.ThrowOnFailure(windowFrame.Show());
    }
  }
}